package com.example.demo.controller;


import java.util.Iterator;


import javax.print.attribute.standard.Severity;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.DateUtils;
import com.example.demo.entity.Order_Details;
import com.example.demo.entity.Order_Main;
import com.example.demo.entity.User;
import com.example.demo.entity.Desk;
import com.example.demo.entity.Food;
import com.example.demo.service.Order_Main_Service;
import com.example.demo.service.Desk_Service;
import com.example.demo.utils.ResultInfo;
import com.example.demo.utils.Searchinfo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@RequestMapping("Order_Main")
@RestController
public class Order_Main_Controller extends Basic_Controller<Order_Main> {

    @Autowired
    Order_Main_Service service;


    @RequestMapping("fooddetails")
    public ResultInfo fooddetails(Integer search, Integer status) {
        //类别里面的商品信息+状态数量+数量
        //id+商品信息+状态+数量
        Food f = new Food();
        f.setFoodtype_id(search == null ? 1 : search);
        f.setStatus(status == null ? 0 : status);
        return new ResultInfo(f.getFoodtype_id(), service.getFoodByType(f));

    }


    public ResultInfo index(Searchinfo info) {
        //int status=0;
        if (info.getSearch() == null || info.getSearch().length() == 0) info.setSearch("0");
        ResultInfo r = super.index(info);
        r.setStatus(Integer.valueOf(info.getSearch()));
        return r;

    }

    @RequestMapping("complet")


    public ResultInfo complet(int id, Integer status, HttpSession session) throws Exception {

        Order_Details de = service.getDetailsById(id);
        de.setStatus(status == null ? 1 : status);
        de.setChanger_date(DateUtils.getNow());
        User u = (User) session.getAttribute("user");
        de.setChanger_user_id(u.getId());
        service.updatedetails(de);

        return new ResultInfo(1);

    }

    @RequestMapping("orderfooddetails")
    public ResultInfo orderfooddetails(Order_Details d, Integer search) {
        //类别里面的商品信息+状态数量+数量
        //id+商品信息+状态+数量
        d.setStatus(search == null ? 0 : search);
        return new ResultInfo(d.getStatus(), service.getFoodBystatus(d));

    }

    @RequestMapping("orderdetails_status")
    public ResultInfo ordertails_status() {


        return new ResultInfo(1, Order_Details.statuss);
    }


    @RequestMapping("details")
    public ResultInfo details(int order_id) {
        //int status=0;

        return new ResultInfo(1, service.selectdetails(order_id));

    }

    //
    @RequestMapping("out")


    public ResultInfo out(int id, int order_id, HttpSession session) throws Exception {
        Order_Main or = service.getById(order_id);

        Order_Details de = service.getDetailsById(id);
        //判断可不可以退？

        if (or.getStatus() > 2) {
            System.out.println(or.getStatus());
            return new ResultInfo(-1, "订单已经" + or.getStatusname() + ",不能全部退菜！");

        }
        if (de.getStatus() > 2) {

            return new ResultInfo(-1, "菜品已经" + de.getStatusname() + ",不能退菜！");

        }
        //更新主表


        or.setReamount(or.getReamount() + de.getAmount());
        or.setAmount(or.getAmount() - de.getAmount());
        service.update(or);

        //更新明细表


        de.setStatus(3);
        de.setChanger_date(DateUtils.getNow());
        User u = (User) session.getAttribute("user");
        de.setChanger_user_id(u.getId());
        service.updatedetailsByOrder(de);

        return new ResultInfo(1);

    }

    @RequestMapping("outall")


    public ResultInfo outall(int id, HttpSession session) throws Exception {
        Order_Main or = service.getById(id);


        //判断可不可以退？

        if (or.getStatus() > 0) {

            return new ResultInfo(-1, "订单已经" + or.getStatusname() + ",不能全部退费！");


        }
        //更新主表

        or.setStatus(4);
        or.setReamount(or.getAmount());
        or.setAmount(0);
        service.update(or);

        //更新明细表
        Order_Details d = new Order_Details();
        d.setOrder_id(id);
        d.setStatus(3);
        d.setChanger_date(DateUtils.getNow());
        User u = (User) session.getAttribute("user");
        d.setChanger_user_id(u.getId());
        service.updatedetailsByOrder(d);

        return new ResultInfo(1);

    }

    @RequestMapping("save")
    public ResultInfo save(String json) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode obj = mapper.readTree(json);
        Double allamount = obj.get("allamount").asDouble();
        Double allcount = obj.get("allcount").asDouble();
        int desk_id = obj.get("deskno").asInt();
        Order_Main main = new Order_Main();
        main.setCode(DateUtils.getCode());
        main.setDate(DateUtils.getNow());
        main.setDesk_id(desk_id);
        main.setAmount(allamount);
        service.insert(main);
        int mainid = main.getId();

        JsonNode items = obj.get("items");
        Iterator<String> names = items.fieldNames();
        while (names.hasNext()) {
            JsonNode item = items.get(names.next());
            int id = item.get("id").asInt();
            double price = item.get("price").asDouble();
            double count = item.get("count").asDouble();


            Order_Details od = new Order_Details();
            od.setOrder_id(mainid);
            od.setFood_id(id);
            od.setCount(count);
            od.setPrice(price);
            od.setAmount(count * price);

            od.setChanger_user_id(0);

            od.setChanger_date(DateUtils.getNow());
            service.insertdetails(od);

        }


        return new ResultInfo(1);


    }


}
